﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;
using Toenda.TFL.Drawing;

public partial class Controls_Main_ctrl_exif : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private ServerPath serverpath = new ServerPath();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool isLoggedIn = false;
		bool letView = false;

		Account tmp = new Account();

		if(ip.CheckImageExists(Request.QueryString.Get("photo"))) {
			if(Session.Count > 0 
			&& ap.CheckSessionExist(Session["Zappr"].ToString())) {
				tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
				isLoggedIn = true;
			}

			if(Request.QueryString.Get("photo") != null) {
				ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
				Account acc = ap.GetAccount(item.UserID);

				// check privacy
				if(isLoggedIn && item.UserID == tmp.ID) {
					if(item.Access > 0) {
						// this time: only the user itself
						// next time: friends and family
						if(item.UserID == acc.ID)
							letView = true;
					}
					else
						letView = true;
				}
				else {
					if(item.Access == 0)
						letView = true;
				}

				/*
				 * image
				 */

				if(letView) {
					lt.Text = GetLocalResourceObject("Title").ToString();
					lt.Text += ": ";
					lt.Text += item.Name;

					//linkCancel.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
					linkCancel.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

					try {
						ExifReader er = new ExifReader(
							serverpath.Path + @"/Images/" + item.ImageOriginal
						);

						// save the exif data to db
						if(item.EquipmentMaker == null
						|| item.EquipmentMaker.Trim() == ""
						|| item.EquipmentModel == null
						|| item.EquipmentModel.Trim() == "") {
							item.EquipmentMaker = er.EquipmentMaker;
							item.EquipmentModel = er.EquipmentModel;

							this.ip.SaveImage(item);
						}

						// display the exif data
						if(er.Title.ToString() != "" && er.Title.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("TitleEXIF").ToString(),
								er.Title.ToString()
							);
						}

						if(er.Description != "" && er.Description != null) {
							this.AddRow(
								GetLocalResourceObject("Description").ToString(),
								er.Description
							);
						}

						if(er.Encoding.EncodingName != "" && er.Encoding.EncodingName != null) {
							this.AddRow(
								GetLocalResourceObject("Encoding").ToString(),
								er.Encoding.EncodingName
							);
						}

						if(er.EquipmentMaker != "" && er.EquipmentMaker != null) {
							this.AddRow(
								GetLocalResourceObject("EquipmentMaker").ToString(),
								er.EquipmentMaker
							);
						}

						if(er.EquipmentModel != "" && er.EquipmentModel != null) {
							this.AddRow(
								GetLocalResourceObject("EquipmentModel").ToString(),
								er.EquipmentModel
							);
						}

						if(er.Software != "" && er.Software != null) {
							this.AddRow(
								GetLocalResourceObject("Software").ToString(),
								er.Software
							);
						}

						if(er.Aperture.ToString() != "" && er.Aperture.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("Aperture").ToString(),
								er.Aperture.ToString()
							);
						}

						if(er.Artist != "" && er.Artist != null) {
							this.AddRow(
								GetLocalResourceObject("Artist").ToString(),
								er.Artist
							);
						}

						if(er.Copyright != "" && er.Copyright != null) {
							this.AddRow(
								GetLocalResourceObject("Copyright").ToString(),
								er.Copyright
							);
						}

						if(er.DateTimeDigitized.ToString() != ""
						&& er.DateTimeDigitized.ToString() != null
						&& er.DateTimeDigitized.ToString() != "01.01.0001 00:00:00") {
							this.AddRow(
								GetLocalResourceObject("DateTimeDigitized").ToString(),
								er.DateTimeDigitized.ToString()
							);
						}

						if(er.DateTimeLastModified.ToString() != ""
						&& er.DateTimeLastModified.ToString() != null
						&& er.DateTimeLastModified.ToString() != "01.01.0001 00:00:00") {
							this.AddRow(
								GetLocalResourceObject("DateTimeLastModified").ToString(),
								er.DateTimeLastModified.ToString()
							);
						}

						if(er.DateTimeOriginal.ToString() != ""
						&& er.DateTimeOriginal.ToString() != null
						&& er.DateTimeOriginal.ToString() != "01.01.0001 00:00:00") {
							this.AddRow(
								GetLocalResourceObject("DateTimeOriginal").ToString(),
								er.DateTimeOriginal.ToString()
							);
						}

						if(er.ExposureMeteringMode.ToString() != "" && er.ExposureMeteringMode.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ExposureMeteringMode").ToString(),
								er.ExposureMeteringMode.ToString()
							);
						}

						if(er.ExposureProgram.ToString() != "" && er.ExposureProgram.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ExposureProgram").ToString(),
								er.ExposureProgram.ToString()
							);
						}

						if(er.ExposureTime.ToString() != "" && er.ExposureTime.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ExposureTime").ToString(),
								er.ExposureTime.ToString()
							);
						}

						if(er.FlashMode.ToString() != "" && er.FlashMode.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("FlashMode").ToString(),
								er.FlashMode.ToString()
							);
						}

						if(er.FocalLength.ToString() != "" && er.FocalLength.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("FocalLength").ToString(),
								er.FocalLength.ToString()
							);
						}

						if(er.Width.ToString() != "" && er.Width.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("Width").ToString(),
								er.Width.ToString()
							);
						}

						if(er.Height.ToString() != "" && er.Height.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("Height").ToString(),
								er.Height.ToString()
							);
						}

						if(er.ISO.ToString() != "" && er.ISO.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ISO").ToString(),
								er.ISO.ToString()
							);
						}

						if(er.LightSource.ToString() != "" && er.LightSource.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("LightSource").ToString(),
								er.LightSource.ToString()
							);
						}

						if(er.Orientation.ToString() != "" && er.Orientation.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("Orientation").ToString(),
								er.Orientation.ToString()
							);
						}

						if(er.ResolutionX.ToString() != "" && er.ResolutionX.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ResolutionX").ToString(),
								er.ResolutionX.ToString()
							);
						}

						if(er.ResolutionY.ToString() != "" && er.ResolutionY.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("ResolutionY").ToString(),
								er.ResolutionY.ToString()
							);
						}

						if(er.SubjectDistance.ToString() != "" && er.SubjectDistance.ToString() != null) {
							this.AddRow(
								GetLocalResourceObject("SubjectDistance").ToString(),
								er.SubjectDistance.ToString()
							);
						}

						if(er.UserComment != "" && er.UserComment != null) {
							this.AddRow(
								GetLocalResourceObject("UserComment").ToString(),
								er.UserComment
							);
						}
					}
					catch(Exception ex) {
						string s = ex.Message;
					}
				}
			}
		}
	}

	/// <summary>
	/// AddRow
	/// </summary>
	/// <param name="title"></param>
	/// <param name="value"></param>
	private void AddRow(string title, string value) {
		Panel master = new Panel();
		master.CssClass = "divBoxGreyBG";

		// left
		Panel left = new Panel();
		left.CssClass = "divTextBox";

		Literal litleft = new Literal();
		litleft.Text = title;

		left.Controls.Add(litleft);
		master.Controls.Add(left);

		// right
		Panel right = new Panel();
		right.CssClass = "divInputBox";

		TextBox txt = new TextBox();
		txt.ReadOnly = true;
		txt.CssClass = "inputDefaultWide";
		txt.TextMode = TextBoxMode.SingleLine;
		txt.Text = value;

		right.Controls.Add(txt);
		master.Controls.Add(right);

		// finish
		infobox.Controls.Add(master);
	}
}
